//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.sys;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicAdmAreaCodeDao;
import com.aidong.dao.wills.DicSalesCodeDao;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.SysMenuDao;
import com.aidong.dao.wills.SysUserDao;
import com.aidong.model.MenuAuthority;
import com.aidong.model.wills.DicSalesCode;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.SysUser;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"sysUser"})
public class SysUserController extends BaseController {
    @Resource
    SysUserDao sysUserDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    DicAdmAreaCodeDao dicAdmAreaCodeDao;
    @Resource
    SysMenuDao sysMenuDao;
    @Resource
    DicSalesCodeDao dicSalesCodeDao;

    public SysUserController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("sys/user/list")).addObject("userGroup", this.getTypeList("userGroup"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "group",required = false) String group, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.gt("status", Integer.valueOf(0));
        cd.eq("groupCode", group);
        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("userCode", "%" + name + "%");
            mv.like("chName", "%" + name + "%");
            mv.like("enName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.sysUserDao.queryPageList(pg, cd, (new OrderSet()).desc("lastLogTime").asc("userCode"));
        ModelAndView mv1 = new ModelAndView("sys/user/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        SysUser user = new SysUser();
        user.setExpire(Integer.valueOf(0));
        user.setSecuityLevel("1");
        user.setStatus(Integer.valueOf(1));
        user.setAllowIp("*.*.*.*");
        List shopList = this.dicShopCodeDao.getAllShopList();
        Iterator areaList = shopList.iterator();

        while(areaList.hasNext()) {
            DicShopCode mv = (DicShopCode)areaList.next();
            mv.setStatus(Integer.valueOf(0));
            mv.setMakeCard(Integer.valueOf(0));
        }

        List areaList1 = this.dicAdmAreaCodeDao.getAllAreaList();
        ModelAndView mv1 = new ModelAndView("sys/user/edit");
        mv1.addObject("user", user);
        mv1.addObject("userGroup", this.getTypeList("userGroup"));
        mv1.addObject("gender", this.getTypeList("gender"));
        mv1.addObject("secuityLevel", this.getTypeList("secuityLevel"));
        mv1.addObject("userDepart", this.getTypeList("userDepart"));
        mv1.addObject("userPosition", this.getTypeList("userPosition"));
        mv1.addObject("shopList", shopList);
        mv1.addObject("areaList", areaList1);
        mv1.addObject("_oper", "add");
        return mv1;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("user") SysUser user, @RequestParam(value = "shops",required = false) String shops, @RequestParam(value = "shopdf",required = false) String shopdf, @RequestParam(value = "areas",required = false) String areas) {
        if(this.chkNull(user.getUserCode())) {
            return this.getResult(false, "请输入用户编码");
        } else if(this.chkNull(user.getPassword())) {
            return this.getResult(false, "请输入密码");
        } else if(this.chkNull(user.getChName())) {
            return this.getResult(false, "请输入姓名");
        } else {
            if(this.chkNull(user.getExpire())) {
                user.setExpire(Integer.valueOf(0));
            }

            if(this.chkNull(user.getStatus())) {
                user.setStatus(Integer.valueOf(1));
            }

            if(this.chkNull(user.getSecuityLevel())) {
                user.setSecuityLevel("1");
            }

            if(this.chkNull(user.getLoginSingle())) {
                user.setLoginSingle(Integer.valueOf(0));
            }

            if(this.chkNull(user.getLoginAllShop())) {
                user.setLoginAllShop(Integer.valueOf(0));
            }

            if(this.chkNull(user.getQueryOwnData())) {
                user.setQueryOwnData(Integer.valueOf(0));
            }

            if(this.chkNull(user.getQueryAllShop())) {
                user.setQueryAllShop(Integer.valueOf(0));
            }

            if(this.sysUserDao.chkExist(user.getUserCode())) {
                return this.getResult(false, "用户编码已存在");
            } else {
                user.setPassword(this.getEncodePwd(user.getUserCode(), user.getPassword()));
                user.setLoginTimes(Integer.valueOf(0));
                user.setPwdTime(new Date(System.currentTimeMillis() - 31536000000L));
                user.setCreateUser(this.getLoginUserCode());
                user.setCreateTime(new Date());
                user.setUpdateTime(new Date());
                Map shopList = this.getShopList(shops, shopdf);
                ArrayList areaList = new ArrayList();
                if(!this.chkNull(areas)) {
                    String[] e = areas.split(",");
                    String[] ee = e;
                    int len$ = e.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        String s = ee[i$];
                        if(!this.chkNull(s)) {
                            areaList.add(s.trim());
                        }
                    }
                }

                try {
                    this.sysUserDao.add(user);
                    this.sysUserDao.updateUserShop(user.getUserCode(), shopList);
                    this.sysUserDao.updateUserArea(user.getUserCode(), areaList);
                    this.reloadTypeList("userName");
                    this.addUserLog("新增用户", "编号:" + user.getUserCode() + ",姓名:" + user.getChName());
                    if(("02".equals(user.getUserPosition()) || "03".equals(user.getUserPosition())) && !this.chkNull(user.getDeptCode()) && !this.dicSalesCodeDao.exists(user.getUserCode())) {
                        DicSalesCode var14 = new DicSalesCode();
                        var14.setCode(user.getUserCode());
                        var14.setChName(user.getChName());
                        var14.setShopCode(user.getDeptCode());
                        var14.setPosition(this.cache.getValue("userPosition", user.getUserPosition()));
                        var14.setStatus(Integer.valueOf(1));
                        var14.setCreateUser(this.getLoginUserCode());
                        var14.setCreateTime(new Date());
                        var14.setUpdateTime(new Date());

                        try {
                            this.dicSalesCodeDao.add(var14);
                            this.reloadTypeList("salesName");
                            this.addUserLog("新增销售员", "编号:" + var14.getCode() + ",名称:" + var14.getChName());
                        } catch (Exception var12) {
                            this.log.error(var12);
                        }
                    }
                } catch (Exception var13) {
                    this.log.error("add sysuser", var13);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    private Map<String, Integer> getShopList(String shops, String shopdf) {
        HashMap dfMap = new HashMap();
        String[] ss;
        int len$;
        if(!this.chkNull(shopdf)) {
            String[] shopList = shopdf.split(",");
            ss = shopList;
            int arr$ = shopList.length;

            for(len$ = 0; len$ < arr$; ++len$) {
                String i$ = ss[len$];
                if(!this.chkNull(i$)) {
                    dfMap.put(i$.trim(), Integer.valueOf(1));
                }
            }
        }

        HashMap var10 = new HashMap();
        if(!this.chkNull(shops)) {
            ss = shops.split(",");
            String[] var11 = ss;
            len$ = ss.length;

            for(int var12 = 0; var12 < len$; ++var12) {
                String s = var11[var12];
                if(!this.chkNull(s)) {
                    var10.put(s.trim(), Integer.valueOf(dfMap.containsKey(s.trim())?1:0));
                }
            }
        }

        return var10;
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("userCode") String userCode) {
        SysUser user = (SysUser)this.sysUserDao.getById(userCode);
        if(user == null) {
            return this.showAlert("没有这个用户");
        } else {
            List shopList = this.sysUserDao.getUserShopList(userCode, true);
            List areaList = this.sysUserDao.getUserAreaList(userCode, true);
            ModelAndView mv = new ModelAndView("sys/user/edit");
            mv.addObject("user", user);
            mv.addObject("userGroup", this.getTypeList("userGroup"));
            mv.addObject("gender", this.getTypeList("gender"));
            mv.addObject("secuityLevel", this.getTypeList("secuityLevel"));
            mv.addObject("userDepart", this.getTypeList("userDepart"));
            mv.addObject("userPosition", this.getTypeList("userPosition"));
            mv.addObject("shopList", shopList);
            mv.addObject("areaList", areaList);
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("user") SysUser user, @RequestParam(value = "shops",required = false) String shops, @RequestParam(value = "shopdf",required = false) String shopdf, @RequestParam(value = "areas",required = false) String areas) {
        if(this.chkNull(user.getUserCode())) {
            return this.getResult(false, "没有这个用户");
        } else if(this.chkNull(user.getChName())) {
            return this.getResult(false, "请输入姓名");
        } else {
            if(this.chkNull(user.getExpire())) {
                user.setExpire(Integer.valueOf(0));
            }

            if(this.chkNull(user.getStatus())) {
                user.setStatus(Integer.valueOf(1));
            }

            if(this.chkNull(user.getSecuityLevel())) {
                user.setSecuityLevel("1");
            }

            if(this.chkNull(user.getLoginSingle())) {
                user.setLoginSingle(Integer.valueOf(0));
            }

            if(this.chkNull(user.getLoginAllShop())) {
                user.setLoginAllShop(Integer.valueOf(0));
            }

            if(this.chkNull(user.getQueryOwnData())) {
                user.setQueryOwnData(Integer.valueOf(0));
            }

            if(this.chkNull(user.getQueryAllShop())) {
                user.setQueryAllShop(Integer.valueOf(0));
            }

            SysUser user1 = (SysUser)this.sysUserDao.getById(user.getUserCode());
            if(user1 == null) {
                return this.getResult(false, "没有这个用户");
            } else {
                if(!this.chkNull(user.getPassword())) {
                    user1.setPassword(this.getEncodePwd(user1.getUserCode(), user.getPassword()));
                }

                user1.setChName(user.getChName());
                user1.setEnName(user.getEnName());
                user1.setGroupCode(user.getGroupCode());
                user1.setDeptCode(user.getDeptCode());
                user1.setUserPosition(user.getUserPosition());
                user1.setMobile(user.getMobile());
                user1.setPhone(user.getPhone());
                user1.setGender(user.getGender());
                user1.setBirthday(user.getBirthday());
                user1.setStaffId(user.getStaffId());
                user1.setExpire(user.getExpire());
                user1.setExpireDate(user.getExpireDate());
                user1.setSecuityLevel(user.getSecuityLevel());
                user1.setStatus(user.getStatus());
                user1.setAllowIp(user.getAllowIp());
                user1.setLoginSingle(user.getLoginSingle());
                user1.setLoginAllShop(user.getLoginAllShop());
                user1.setQueryOwnData(user.getQueryOwnData());
                user1.setQueryAllShop(user.getQueryAllShop());
                user1.setUpdateTime(new Date());
                Map shopList = this.getShopList(shops, shopdf);
                ArrayList areaList = new ArrayList();
                if(!this.chkNull(areas)) {
                    String[] e = areas.split(",");
                    String[] arr$ = e;
                    int len$ = e.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        String s = arr$[i$];
                        if(!this.chkNull(s)) {
                            areaList.add(s.trim());
                        }
                    }
                }

                try {
                    this.sysUserDao.update(user1);
                    this.sysUserDao.updateUserShop(user.getUserCode(), shopList);
                    this.sysUserDao.updateUserArea(user.getUserCode(), areaList);
                    this.reloadTypeList("userName");
                    this.addUserLog("修改用户", "编号:" + user1.getUserCode() + ",姓名:" + user1.getChName());
                } catch (Exception var13) {
                    this.log.error("edit sysuser", var13);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("userCode") String userCode) {
        SysUser user = (SysUser)this.sysUserDao.getById(userCode);
        if(user == null) {
            return this.getResult(false, "没有这个用户");
        } else {
            try {
                this.sysUserDao.updateStatus(userCode, 0);
                this.reloadTypeList("userName");
                this.addUserLog("删除用户", "编号:" + user.getUserCode() + ",姓名:" + user.getChName());
            } catch (Exception var4) {
                this.log.error("del sysuser", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }

    @RequestMapping({"auth"})
    public ModelAndView auth(@RequestParam("userCode") String userCode) {
        SysUser user = (SysUser)this.sysUserDao.getById(userCode);
        if(user == null) {
            return this.showAlert("没有这个用户");
        } else {
            List list = this.sysMenuDao.getUserMenuAuthorityList(user.getUserCode());
            ArrayList plist = new ArrayList();
            ArrayList mlist = new ArrayList();
            HashMap parentMap = new HashMap();
            Iterator map1 = list.iterator();

            while(true) {
                MenuAuthority mv;
                do {
                    if(!map1.hasNext()) {
                        HashMap map11 = new HashMap();

                        while(parentMap.size() > 0) {
                            map11.clear();
                            Iterator mv1 = list.iterator();

                            while(mv1.hasNext()) {
                                MenuAuthority m2 = (MenuAuthority)mv1.next();
                                if(parentMap.containsKey(m2.getParentId())) {
                                    mlist.add(m2);
                                    map11.put(m2.getMenuId(), m2);
                                }
                            }

                            parentMap.clear();
                            parentMap.putAll(map11);
                        }

                        ModelAndView mv2 = new ModelAndView("sys/user/auth");
                        mv2.addObject("user", user);
                        mv2.addObject("plist", plist);
                        mv2.addObject("mlist", mlist);
                        return mv2;
                    }

                    mv = (MenuAuthority)map1.next();
                } while(mv.getParentId().intValue() != 0);

                plist.add(mv);
                Iterator m = list.iterator();

                while(m.hasNext()) {
                    MenuAuthority m1 = (MenuAuthority)m.next();
                    if(m1.getParentId().intValue() == mv.getMenuId().intValue()) {
                        plist.add(m1);
                        parentMap.put(m1.getMenuId(), m1);
                    }
                }
            }
        }
    }

    @RequestMapping({"_auth"})
    @ResponseBody
    public String _auth(@RequestParam("userCode") String userCode, HttpServletRequest request) {
        SysUser user = (SysUser)this.sysUserDao.getById(userCode);
        if(user == null) {
            return this.getResult(false, "没有这个用户");
        } else {
            HashMap authMap = new HashMap();

            try {
                Enumeration e = request.getParameterNames();

                while(e.hasMoreElements()) {
                    String name = (String)e.nextElement();
                    if(name.startsWith("auth_")) {
                        Integer mid = this.parseInt(name.substring(5));
                        if(!this.chkNullZ(mid)) {
                            int pp = 0;
                            String[] vv = request.getParameterValues(name);
                            String[] arr$ = vv;
                            int len$ = vv.length;

                            for(int i$ = 0; i$ < len$; ++i$) {
                                String v = arr$[i$];
                                pp += this.parseInt(v).intValue();
                            }

                            authMap.put(mid, Integer.valueOf(pp));
                        }
                    }
                }

                this.sysMenuDao.updateUserAuthority(userCode, authMap);
                this.addUserLog("设置用户权限", "编号:" + user.getUserCode() + ",姓名:" + user.getChName());
                return this.getResult(true, "保存完成");
            } catch (Exception var14) {
                this.log.error("edit sysuser auth", var14);
                return this.getResult(false, "保存发生错误");
            }
        }
    }
}
